import axios from 'axios'

//
axios.defaults.baseURL = 'http://localhost:8888/api/private/v1/'

//拦截器(除了login)
axios.interceptors.request.use(function (config) {
    var token=localStorage.getItem('mytoken')
    if(token){
         //设置请求头(http 请求头携带Authorization参数)   
         config.headers['Authorization'] = token
    }
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
});

//登录验证
// export const checkUser = function (params){
//     return axios.post('login', params).then(function(res){
//         return res.data
//     })
// }

export const checkUser = (params)=>{
    return axios.post('login',params).then((res)=>res.data);
}

//获取用户列表
export const getUserList = (params) =>{
    return axios.get('users',params).then((res)=>res.data) 
}
//更改用户状态
export const changeUserState = params => {
  //用户id和状态 
  return axios.put(`users/${params.uid}/state/${params.type}`).then((res)=>res.data)
}

//添加用户
export const addUser = (params) =>{
  return axios.post('users',params).then((res)=>res.data);
}

//根据id显示查找用户数据
export const getUserById = (params) => {
  return axios.get(`users/${params.id}`).then((res)=>res.data)
}

//修改用户
export const editUser = (params) =>{
  return axios.put(`users/${params.id}`,params).then((res)=>res.data)
}

//用户删除
export const deleteUser = (params) =>{
  return axios.delete(`users/${params}`).then((res)=>res.data);
}

//分配用户角色
export const grantUserRole = (id,rid) => {
      
  return axios.put(`users/${id}/role`, rid).then(res=>res.data)
}

//权限列表
export const getAuthList = (params) => {
  return axios.get(`rights/${params.type}`).then(res=>res.data)
}

//角色列表
export const getRoleList = (params) => {
  return axios.get('roles').then(res=>res.data)
}

//角色添加
export const addRole = (params)=> {
  return axios.post('roles', params).then(res=>res.data)
}

//根据id获取角色信息
export const getRoleById = (params) => {
  return axios.get(`roles/${params.id}`).then(res=>res.data)
}
//修改角色
export const editRole = (params) => {
  return axios.put(`roles/${params.id}`,params).then(res=>res.data)
}
//删除角色
export const delRole = (params) => {
  return axios.delete(`roles/${params}`).then(res=>res.data)
}

//删除角色指定权限
export const deleteRoleAuth = params => {
  return axios.delete(`roles/${params.roleId}/rights/${params.rightId}`).then(res=>res.data)
}

//角色授权
export const grantRoleAuth = (roleId, rids) => {
  return axios.post(`roles/${roleId}/rights`,rids).then(res=>res.data)
}

// export const getGoodssList = (typeNum) => {
//   return axios.get('categories',{type:typeNum}).then(res=>res.data)
// }

//获取商品分类列表
export const getGoodsCateList = (typeNum,pagenum,pagesize) => {
  return axios.get('categories', {params:{type:typeNum,pagenum:pagenum,pagesize:pagesize}}).then(res=>res.data)
}

//获取所有分类
export const getCategoryList = (params) =>{
  return axios.get(`categories`,params).then(res=>res.data)
}


//添加分类
export const addCategories = params => {
  return axios.post('categories', params).then(res=>res.data)
}

//菜单数据
export const getMenus = () => {
  return axios.get('menus').then(res=>res.data)
}

//获取商品列表
export const getGoodsList = params => {
  return axios.get('goods',params).then(res=>res.data)
}

//商品添加
export const addGoods = params => {
  return axios.post('goods', params).then(res=>res.data)
}

